package root.electronclass.electronclass.grade.excellent_students

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import root.electronclass.electronclass.R
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassExcellentStudentsItemBinding
import root.electronclass.electronclass.databinding.FragmentClassListBinding
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.doOnTabSelected
import root.electronclass.electronclass.ext.dp
import root.electronclass.electronclass.ext.showAvatar
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.grade.model.Person
import root.electronclass.electronclass.grade.model.Semester
import root.electronclass.electronclass.mvp.rest.ClassApi

/**
 * 班级优秀生
 * @author Lokiy
 * @date 2023/2/5 13:19
 */
class ClassExcellentStudentsFragment : ViewBindingFragment<FragmentClassListBinding>() {

    private val viewModel: ClassExcellentStudentsViewModel by viewModels()

    @Suppress("DEPRECATION")
    private val adapter = BasicAdapter<Person, FragmentClassExcellentStudentsItemBinding> { holder, item ->
        holder.binding.itemName.text = item.name
        holder.binding.itemNo.text = getString(R.string.class_student_no, item.no)
        holder.binding.itemFav.text = buildSpannedString { bold { append(getString(R.string.class_excellent_students_fav)) }; append(item.fav) }
        holder.binding.itemSpecialty.text =
            buildSpannedString { bold { append(getString(R.string.class_excellent_students_specialty)) }; append(item.specialty) }
        holder.binding.itemSlogan.text =
            buildSpannedString { bold { append(getString(R.string.class_excellent_students_slogan)) }; append(item.slogan) }
        holder.binding.itemStory.text = buildSpannedString { bold { append(getString(R.string.class_excellent_students_story)) }; append(item.story) }
        holder.binding.itemEvaluation.text =
            buildSpannedString { bold { append(getString(R.string.class_excellent_students_evaluation)) }; append(item.evaluation) }
        holder.binding.itemAvatar.showAvatar(item.avatar, if (item.gender == 1) 3 else 2)
        holder.binding.root.setOnClickListener {
            //详情
            startFragment(ClassExcellentStudentsDetailFragment::class.java, bundleOf("_DATA" to item))
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        lifecycleScope.launch(CoroutineExceptionHandler()) {
            binding.tabLayout.doOnTabSelected {
                val tag = it?.tag
                if (tag is Semester) {
                    viewModel.loadData(tag)
                }
            }
            withContext(Dispatchers.IO) {
                ClassApi.service.getTermList()?.rows
            }?.forEach {
                binding.tabLayout.addTab(binding.tabLayout.newTab().setCustomView(R.layout.view_tab_item).setTag(it).setText(it.termName).apply {
                    val customView = customView ?: return@apply
                    if (customView is TextView) {
                        customView.text = it.termName
                    }
                })
            }?.run {
                binding.tabLayout.getTabAt(0)?.select()
            }

        }

        binding.recycler.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            marginStart = dp2px(80.dp).toInt()
            marginEnd = dp2px(80.dp).toInt()
        }
        binding.recycler.layoutManager = GridLayoutManager(requireContext(), 3)
        binding.recycler.addItemDecoration(GridSpacingItemDecoration(3, dp2px(24.dp).toInt(), false))
        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.loadData(key = it)
            false
        }

        viewModel.data.observe(viewLifecycleOwner) {
            adapter.notifyChanged(it)
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.loadData()
        }
    }
}
